<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 头部的布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="404" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item
                      @click.native="hAdd('')"
                    >添加子部门</el-dropdown-item>
                    <el-dropdown-item>编辑部门</el-dropdown-item>
                    <el-dropdown-item>删除部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 头部布局end -->

        <!-- 树 -->
        <el-tree :data="list" :default-expand-all="true">
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!--
      #default === slot-scope
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (类似于table 单元格数据渲染)
    -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>

              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="hAdd(data.id)"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="hEdit(data.id)"
                        >编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.pid !== '' && data.children.length===0 " @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- 准备弹层放入新增部门的子组件 -->
      <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        :destroy-on-close="true"
        @close="hClose"
      >
        <!-- 新增子部门或编辑部门的子组件 -->
        <!--  v-if="dialogVisible" 解决数据不更新的方式1 -->
        <addOrEdit :id="curId" ref="myDept" :is-edit="isEdit" :origin-list="originList" @success="hSuccess" @close="dialogVisible = false" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartments, delUserInfoByID } from '@/api/departments'
import { tranListToTreeList } from '@/utils'
import addOrEdit from './deptDialog.vue'
export default {
  name: 'DepartmentsPage',
  components: {
    addOrEdit
  },
  data() {
    return {
      dialogVisible: false,
      list: [],
      curId: '', // 父级id
      isEdit: false, // 用于判断提交是否为 添加或者修改
      originList: []
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    hClose() {
      this.$refs.myDept.resetForm()
    },
    hSuccess() {
      // 关闭和刷新
      this.dialogVisible = false
      this.loadDepartments()
    },
    // 新增数据
    hAdd(id) {
      this.isEdit = false
      this.curId = id
      this.dialogVisible = true
    },
    // 修改数据
    hEdit(id) {
      this.isEdit = true
      this.curId = id
      this.dialogVisible = true
      // 解决数据不更新的方式2，在父组件中调用子组件中的方法
      this.$nextTick(() => {
        this.$refs.myDept.loadDepartmentDetail()
      })
    },
    // 删除
    hDel(id) {
      this.$confirm('您确定删除此部门吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(async() => {
        const res = await delUserInfoByID(id)
        console.log('删除员工成功', res)
        if (res.data.success) {
          // 重新刷新页面
          this.loadDepartments()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 获取tree数据
    async loadDepartments() {
      try {
        const res = await getDepartments()
        console.log('获取tree数据', res)
        const arr = res.data.data.depts
        this.originList = arr.map(item => {
          const { name, code, id, pid } = item
          return { name, code, id, pid }
        })
        this.list = tranListToTreeList(arr)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style></style>
